package com.tools.common.object;

import java.util.Locale;

/**
 * 计算机容量的枚举实例
 * */
@Note("计算机容量的枚举实例")
public enum CapacityEnum {

    B("B"),
    KB("KB"),
    MB("MB"),
    GB("GB"),
    TB("TB"),
    PB("PB");

    private final String value;

    CapacityEnum(String value) {
        this.value = value;
    }

    public String getValue() {
        return value;
    }

    public boolean eq(String value) {
        return this.value.equals(value);
    }

    public boolean eqIgnoreCase(String value) {
        return this.value.equalsIgnoreCase(value);
    }

    @Override
    public String toString() {
        return value;
    }

    @Note("安全地根据 value 值获取对应的枚举实例，获取不到不会抛异常")
    public static CapacityEnum getEnumSafe(String value) {
        switch (value.toUpperCase(Locale.ROOT)) {
            case "B": return B;
            case "KB": return KB;
            case "MB": return MB;
            case "GB": return GB;
            case "TB": return TB;
            case "PB": return PB;
            default: return null;
        }
    }

    @Note("根据 value 值获取对应的枚举实例，获取不到会抛异常")
    public static CapacityEnum getEnum(String value) {
        CapacityEnum res = getEnumSafe(value);
        if(res == null) throw new IllegalArgumentException("不知道 " + value + " 是什么枚举实例");
        return res;
    }

    @Note("将指定单位的 source 转为 this")
    public long cast(long source, CapacityEnum sourceUnit) {
        switch (this) {
            case B: return toB(source, sourceUnit);
            case KB: return toKB(source, sourceUnit);
            case MB: return toMB(source, sourceUnit);
            case GB: return toGB(source, sourceUnit);
            case TB: return toTB(source, sourceUnit);
            case PB: return toPB(source, sourceUnit);
        }
        throw new IllegalArgumentException();
    }


    @Note("将指定单位的容量大小，转为对应的字节大小")
    public static long toB(long source, CapacityEnum sourceUnit) {
        switch (sourceUnit) {
            case B: return source;
            case KB: return source * 1024;
            case MB: return source * 1024 * 1024;
            case GB: return source * 1024 * 1024 * 1024;
            case TB: return source * 1024 * 1024 * 1024 * 1024;
            case PB: return source * 1024 * 1024 * 1024 * 1024 * 1024;
        }
        throw new IllegalArgumentException("不知道 " + sourceUnit + " 是什么枚举实例");
    }


    @Note("将指定单位的容量大小，转为对应的 KB 大小")
    public static long toKB(long source, CapacityEnum sourceUnit) {
        switch (sourceUnit) {
            case B: return source / 1024;
            case KB: return source;
            case MB: return source * 1024;
            case GB: return source * 1024 * 1024;
            case TB: return source * 1024 * 1024 * 1024;
            case PB: return source * 1024 * 1024 * 1024 * 1024;
        }
        throw new IllegalArgumentException("不知道 " + sourceUnit + " 是什么枚举实例");
    }

    @Note("将指定单位的容量大小，转为对应的 MB 大小")
    public static long toMB(long source, CapacityEnum sourceUnit) {
        switch (sourceUnit) {
            case B: return source / 1024 / 1024;
            case KB: return source / 1024;
            case MB: return source;
            case GB: return source * 1024;
            case TB: return source * 1024 * 1024;
            case PB: return source * 1024 * 1024 * 1024;
        }
        throw new IllegalArgumentException("不知道 " + sourceUnit + " 是什么枚举实例");
    }


    @Note("将指定单位的容量大小，转为对应的 GB 大小")
    public static long toGB(long source, CapacityEnum sourceUnit) {
        switch (sourceUnit) {
            case B: return source / 1024 / 1024 / 1024;
            case KB: return source / 1024 / 1024;
            case MB: return source / 1024;
            case GB: return source;
            case TB: return source * 1024;
            case PB: return source * 1024 * 1024;
        }
        throw new IllegalArgumentException("不知道 " + sourceUnit + " 是什么枚举实例");
    }

    @Note("将指定单位的容量大小，转为对应的 TB 大小")
    public static long toTB(long source, CapacityEnum sourceUnit) {
        switch (sourceUnit) {
            case B: return source / 1024 / 1024 / 1024 / 1024;
            case KB: return source / 1024 / 1024 / 1024;
            case MB: return source / 1024 / 1024;
            case GB: return source / 1024;
            case TB: return source;
            case PB: return source * 1024;
        }
        throw new IllegalArgumentException("不知道 " + sourceUnit + " 是什么枚举实例");
    }


    @Note("将指定单位的容量大小，转为对应的 PB 大小")
    public static long toPB(long source, CapacityEnum sourceUnit) {
        switch (sourceUnit) {
            case B: return source / 1024 / 1024 / 1024 / 1024 / 1024;
            case KB: return source / 1024 / 1024 / 1024 /1024;
            case MB: return source / 1024 / 1024 / 1024;
            case GB: return source / 1024 / 1024;
            case TB: return source / 1024;
            case PB: return source;
        }
        throw new IllegalArgumentException("不知道 " + sourceUnit + " 是什么枚举实例");
    }
}
